<template>
  <transition
    enter-active-class="animated short slideInDown"
    leave-active-class="animated short slideOutUp"
  >
    <div class="cont" v-if="show">
      <span class="leftTit"
        ><img src="../assets/img/logo.png" />{{ $t("APP下载") }}</span
      >
      <p v-if="platform === 'apple'">
        <a
          href="javascript:;"
          class="ios"
          @click="showTip = true"
          v-if="isWeiXi || isQQ"
        >
          <img src="../assets/img/appleApp.png" />
        </a>
        <!-- <a :href="'itms-services://?action=download-manifest&url='+url.appleUrl" target="_blank" class="ios" v-else>
                    <img src="../assets/img/appleApp.png"/>
                </a> -->
        <a :href="url" target="_blank" class="ios" v-else>
          <img src="../assets/img/appleApp.png" />
        </a>
      </p>
      <p v-else>
        <a href="javascript:;" @click="showTip = true" v-if="isWeiXi || isQQ">
          <img src="../assets/img/androidApp.png" />
        </a>
        <a
          href="javascript:;"
          @click="downloadApk"
          v-else-if="url.endsWith('.apk')"
        >
          <img src="../assets/img/androidApp.png" />
        </a>
        <a :href="url" target="_blank" v-else>
          <img src="../assets/img/androidApp.png" />
        </a>
      </p>
      <van-icon class="f16" name="cross" @click="show=false" />
    </div>
  </transition>
  <div class="share-layer" @click="showTip = false" v-if="showTip">
    <p class="lh20">
      {{ $t("public0.app_share_tip").format(isWeiXi ? $t("微信") : "QQ") }}
    </p>
  </div>
</template>

<script>
import { getPhonePlatform } from "@/utils";
import { appVersionInfo } from "@/api";
import { useMapState, useComputed } from "@/utils/vuexm";
import { ref, computed, watch, onMounted} from "vue";

export default {
  setup(params, ctx) {
    const platform = getPhonePlatform() == "ios" ? "apple" : "android";
    const isWeiXi = /MicroMessenger\/[0-9]/i.test(window.navigator.userAgent);
    const isQQ = /QQ\/[0-9]/i.test(window.navigator.userAgent);
    const show = ref(false);
    const showTip = ref(false);
    const { sysParams } = useComputed(useMapState("user", ["sysParams"]));

    const url = computed(() => {
      return sysParams.value[`${platform}Url`] || "";
    });

    function downloadApk(){
      if (url.value) {
        let name = url.value.split("/");
        name = name[name.length - 1];
      }
      const _dom = $("<a/>", { href: url.value, download: name });
      _dom.click();
    }

    function showDownload() {
      if (window.isApp) {
        return;
      }
      setTimeout(() => {
        if (url.value) {
          show.value = true;
          setTimeout(() => {
            if (show.value) {
              show.value = false;
            }
          }, 60000);
        }
      }, 3000);
    }

    onMounted(()=>{
        watch(url, _new =>{
            showDownload()
        }, {immediate:true})
    })


    return {
      platform,
      isWeiXi,
      isQQ,
      show,
      showTip,
      url,
      downloadApk
    };
  }
};
</script>

<style scoped lang="less">
.cont {
  background: var(--bg-color);
  height: 0.6rem;
  line-height: 0.5rem;
  padding: 0.05rem 0.15rem;
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 1;
  color: var(--text-color);
  font-size: 0.14rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--light-border-color);
  .leftTit {
    display: flex;
    align-items: center;
    img {
      width: 0.35rem;
      height: 0.35rem;
      margin-right: 0.1rem;
      border-radius: 0.1rem;
    }
  }
  p {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    margin-right: 0.3rem;
    a {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 0.6rem;
      height: 0.35rem;
      background: #f6f6f6;
      border-radius: 0.05rem;
      margin-left: 0.1rem;
      img {
        width: 0.2rem;
      }
    }
    .ios {
      background: #0193d2;
    }
  }
  i {
    position: absolute;
    display: inline-block;
    width: 0.4rem;
    height: 0.6rem;
    text-align: center;
    line-height: 0.6rem;
    font-style: normal;
    right: 0;
    top: 0;
    background-size: 0.09rem;
  }
}
.share-layer {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  color: #fff;
  z-index: 2;
  background: url("../assets/img/share.png") rgba(0, 0, 0, 0.5) no-repeat right top;
  background-size: 3.75rem auto;
}
.share-layer p {
  position: absolute;
  left: 0.3rem;
  right: 0.3rem;
  top: 40%;
  text-align: center;
  font-size: 0.16rem;
  color: #fff;
}
</style>
